﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using LiveEduWizard.Filter;

namespace LiveEduWizard.Views
{
    public partial class FilterPanel : PhoneApplicationPage
    {
        private ApplicationBarIconButton filterButton;

        public FilterPanel()
        {
            InitializeComponent();
            InitApplicationBar();

            for (int i = 0; i < App.ViewModel.SubjectNames.Count; i++)
                SubjectListPicker.Items.Add(App.ViewModel.SubjectNames[i]);

            if (App.ViewModel.FirstAppointmentStart == DateTime.MinValue
                && App.ViewModel.LastAppointmentEnd == DateTime.MinValue)
            {
                FromDatePicker.IsEnabled = false;
                FromTimePicker.IsEnabled = false;
                ToDatePicker.IsEnabled = false;
                ToTimePicker.IsEnabled = false;
            }
            
            FromDatePicker.Value = App.ViewModel.FirstAppointmentStart;
            FromTimePicker.Value = App.ViewModel.FirstAppointmentStart;
            ToDatePicker.Value = App.ViewModel.LastAppointmentEnd;
            ToTimePicker.Value = App.ViewModel.LastAppointmentEnd;
        }

        private void InitApplicationBar()
        {
            ApplicationBar = new ApplicationBar();
            ApplicationBar.Mode = ApplicationBarMode.Default;
            ApplicationBar.IsVisible = true;
            ApplicationBar.IsMenuEnabled = true;
            InitApplicationBarButtons();
        }

        private void InitApplicationBarButtons()
        {
            filterButton = new ApplicationBarIconButton(ThemeHelper.FilterButtonImage);
            filterButton.Text = ThemeHelper.FilterButtonText;
            filterButton.Click += new EventHandler(filterButton_Click);
            ApplicationBar.Buttons.Add(filterButton);

            ApplicationBarIconButton backButton = new ApplicationBarIconButton(ThemeHelper.BackButtonImage);
            backButton.Text = ThemeHelper.BackButtonText;
            backButton.Click += new EventHandler(backButton_Click);
            ApplicationBar.Buttons.Add(backButton);
        }

        void filterButton_Click(object sender, EventArgs e)
        {
            AndFilterRule andRule = new AndFilterRule();

            //Contains Filter
            if (ContainsTextBox.Text != null && ContainsTextBox.Text != string.Empty)
            {
                andRule.FilterRules.AddLast(new ContainsFilterRule((string)ContainsTextBox.Text));
            }

            //Subject Filter
            if (SubjectListPicker.SelectedIndex > 0 && SubjectListPicker.SelectedItem != null)
            {
                andRule.FilterRules.AddLast(new SubjectFilterRule((string)SubjectListPicker.SelectedItem));
            }

            //Type Filter
            if (AppointmentListPicker.SelectedIndex > 0 && AppointmentListPicker.SelectedItem != null)
            {
                andRule.FilterRules.AddLast(new TypeFilterRule((AppointmentType)Enum.Parse(
                    typeof(AppointmentType), (string)AppointmentListPicker.SelectedItem, true)));
            }

            //Range Filter
            DateTime from = GetDateTime(FromDatePicker.Value, FromTimePicker.Value, 0);
            DateTime to = GetDateTime(ToDatePicker.Value, ToTimePicker.Value, 59);
            if (from > App.ViewModel.FirstAppointmentStart || to < App.ViewModel.LastAppointmentEnd)
            {
                andRule.FilterRules.AddLast(new RangeFilterRule(from, to));
            }

            //Apply filter
            App.ViewModel.FilterRule = andRule;

            NavigationService.GoBack();
        }

        private DateTime GetDateTime(DateTime? date, DateTime? time, int second)
        {
            if (!date.HasValue || !time.HasValue)
                return DateTime.MinValue;

            return new DateTime(date.Value.Year, date.Value.Month, date.Value.Day, time.Value.Hour, time.Value.Minute, second);
        }  

        void backButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void Range_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            DateTime from = GetDateTime(FromDatePicker.Value, FromTimePicker.Value, 0);
            DateTime to = GetDateTime(ToDatePicker.Value, ToTimePicker.Value, 59);

            filterButton.IsEnabled = from <= to;
        }
    }
}